#pragma once

#include "common/define/noncopyable.h"
#include <array>
#include <cstring>

/**
 * 固定大小的buffer
 */
template<std::size_t Size>
class FixedBuffer : public noncopyable
{
public:
    FixedBuffer() = default;
    ~FixedBuffer()
    {
        reset();
    }
public:
    std::size_t append(const char* buf, std::size_t size)
    {
        if (m_len + size >= m_data.size())
        {
            size = m_data.size() - m_len;
        }
        memcpy(m_data.data() + m_len, buf, size);
        m_len += size;
        return size;
    }

    std::size_t currLen() const
    { return m_len; }

    std::size_t size() const
    { return m_data.size(); }

    void clear()
    {
        m_data.fill(0);
        m_len = 0;
    }

    void reset()
    { m_len = 0u; }

    const char* data() const
    { return m_data.data(); }

private:
    std::array<char, Size> m_data;
    std::size_t m_len{ 0 };
};